//=============================================================================
// sgGuardProj.
//=============================================================================
class TFProtProjR extends TFProjectile;

var() sound HitSound;
var Actor Seeking;
var vector InitialDir;
var xEmitter Trail;
var class<xEmitter> TrailClass, ExploClass;
var float grade;

replication
{
    reliable if( bNetInitial && (Role==ROLE_Authority) )
        Seeking, InitialDir, grade;
}

simulated function Destroyed()
{
    if (Trail !=None) Trail.mRegen=False;
	Super.Destroyed();
}

simulated function PostBeginPlay()
{
//    local Rotator R;

	if ( Level.NetMode != NM_DedicatedServer )
    {
        Trail = Spawn(trailclass,self,, Location, Rotation);
        Trail.Lifespan = Lifespan;
    }

    PlaySound(sound'TFMedia.tfBCProjShot', SLOT_None,192);
    Spawn(class'TaskForces.tfProtMuz');

    Super.PostBeginPlay();

	Velocity = Vector(Rotation);
    Acceleration = Velocity * 1500.0;
    Velocity *= Speed;


	setTimer(0.1,false);
}

simulated function Timer()
{
    local vector ForceDir, seekloc;
    local float VelMag;

    setTimer(grade,true);

    if(physics==PHYS_None)
    {
        damage=default.damage*(lifespan/0.5)/2;
        blowup(location);
        return;
    }

    if ( InitialDir == vect(0,0,0) )
        InitialDir = Normal(Velocity);

    if (Seeking != None)
    {
		seekloc = seeking.Location + FMin(0.5+frand()*0.5, 1 + 0.8 * FRand()) * (seeking.Velocity * VSize(seeking.location - location)/vsize(velocity));
		seekloc.z-=32;

        ForceDir = Normal(seekloc - Location);

		if( (ForceDir dot InitialDir) > 0 )
		{
			VelMag = VSize(Velocity);

			ForceDir = Normal(ForceDir * 0.5 * VelMag + Velocity);
			Velocity =  VelMag * ForceDir;
		}
		SetRotation(rotator(Velocity));
    }
}

simulated function PostNetBeginPlay()
{
    if ( Level.bDropDetail || Level.DetailMode == DM_Low )
	LightType = LT_None;
}

simulated function Explode(vector HitLocation, vector HitNormal)
{
     if(physics==PHYS_None) return;

    if ( EffectIsRelevant(Location,false) )
	{
        Spawn(ExploClass,,, HitLocation+hitnormal*4, rotator(HitNormal));
        //Spawn(class'TaskForces.TFProtExpB',,, HitLocation+hitnormal*4, rotator(HitNormal));
	}
    PlaySound(HitSound,,128);
    if(trail!=none) trail.Destroy();
    velocity*=0;
    setphysics(PHYS_None);
    lifespan=0.6;
    Blowup(HitLocation+hitnormal*5);
    settimer(0.1,true);
}

function BlowUp(vector HitLocation)
{
	HurtRadius(Damage, DamageRadius, MyDamageType, MomentumTransfer, HitLocation );
    MakeNoise(1.0);
}

simulated function ProcessTouch (Actor Other, vector HitLocation)
{
//    local Vector X, RefNormal, RefDir;

    if (Other == Owner || physics==PHYS_None) return;
    else if(tfstation(other)!=none &&
         tfstation(other).myteam==tfstation(owner).myteam) return;
    else if(pawn(other).playerreplicationinfo!=none &&
         pawn(other).playerreplicationinfo.team.teamindex==tfstation(owner).myteam) return;

    if ( !Other.IsA('Projectile') || Other.bProjTarget )
	{
		Explode(HitLocation, vect(0,0,1));
	}
}

defaultproperties
{
     HitSound=Sound'TFMedia.Buildings.tfBCProjImp'
     TrailClass=Class'taskforces.TFProtTrailR'
     ExploClass=Class'taskforces.TFProtExpR'
     Speed=1000.000000
     MaxSpeed=1500.000000
     Damage=30.000000
     DamageRadius=250.000000
     MomentumTransfer=1000.000000
     MyDamageType=Class'taskforces.DamTypeProtR'
     ExplosionDecal=Class'XEffects.LinkBoltScorch'
     DrawType=DT_None
     LifeSpan=30.000000
     SoundVolume=255
     SoundRadius=50.000000
     ForceType=FT_Constant
     ForceRadius=30.000000
     ForceScale=5.000000
}
